package com.wnn.demo.model.hospital.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wnn.demo.advice.MyException;
import com.wnn.demo.domain.Job;
import com.wnn.demo.model.hospital.mapper.JobMapper;
import com.wnn.demo.pojo.ResultCode;
import com.wnn.demo.utils.IdWorker;
import com.wnn.demo.utils.MyStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * 职位管理
 * @author asus
 */
@Service
public class JobService {

    @Autowired
    private   JobMapper jobMapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查找所有职位信息
     * @param selectJobId
     * @param type
     * @param pId
     * @return
     */
    public List< Map> selectJobAll(String selectJobId, String type, String pId) {
        List<Map> list = jobMapper.findAllJobList(type,pId);
        if (!MyStringUtil.isNullOrEmpty(selectJobId)){
            int id = Integer.parseInt(selectJobId);
            for (Map j:list
                 ) {
                Integer jobId = (Integer) j.get("jobId");
                if ( jobId!=null && jobId==id){
                    j.put("isJob",1);
                }
            }
        }
        return list;
    }

    /**
     * 新增职位
     * @param job
     * @return
     */
    public int insertJob(Job job) {

        Date date = new Date();
        Timestamp timeStamp = new Timestamp(date.getTime());
        Integer type = job.getType();
        //如果职位类型是 父级0
        if (type==0){
            job.setJobRank(0);
            job.setParentId(0);
            BigDecimal nullSalary = new BigDecimal(0);
            job.setAverageSalary(nullSalary);
        }
        job.setStatus(1);
        job.setCreateTime(timeStamp);
        int i = jobMapper.insert(job);
        return i;
    }

    /**
     * 更新职位
     * @param job
     * @return
     */
    public int updateJob(Job job) {

        Job jobById = jobMapper.selectByPrimaryKey(job);
        //从父类变成子类
        if (job.getType()==1 && jobById.getType()==0){
            //查询父类下有无子类，有的情况禁止转换成字职业
            Example example = new Example(Job.class);
            Criteria criteria = example.createCriteria();
            criteria.andEqualTo("parentId",jobById.getJobId());
            List< Job > jobs = jobMapper.selectByExample(example);
            if (jobs.size()>0){
                throw new  MyException(ResultCode.Hava_Child_Menu_Dont_Edit);
            }
        }

        Example example=new Example(Job.class);
        example.createCriteria().andEqualTo("jobId",job.getJobId());
        Integer type = job.getType();
        if (type ==0){
            job.setJobRank(0);
            job.setParentId(0);
            BigDecimal nullSalary = new BigDecimal(-1);
            job.setAverageSalary(nullSalary);
        }
        return jobMapper.updateByExampleSelective(job,example);

    }

    public int deleteJobById(String id) {



        //查询父类下有无子类，有的情况禁止转换成字职业
        Example example = new Example(Job.class);
        Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId",id);
        List< Job > jobs = jobMapper.selectByExample(example);
        if (jobs.size()>0){
            throw new  MyException(ResultCode.Hava_Child_Menu_Dont_Deleted);
        }

        int i = jobMapper.deleteByPrimaryKey(id);
        return i;
    }

    public int batchJobByIds(String ids) {


        return jobMapper.deleteByIds(ids);
    }

    public List< Job > findJobAll() {
        return  jobMapper.selectAll();
    }

    public List<Job > findJobTree() {

        List< Job > jobs = jobMapper.selectAll();

        List< Job > jobParentList = jobs.stream().
                filter(job -> job.getParentId()==0 && job.getType() == 0).collect(Collectors.toList());

        for (Job j:jobParentList
             ) {
            ArrayList< Job > childrenList = new ArrayList<>();
            jobs.stream().filter( job ->  job.getType() == 1 )
                    .forEach( job -> { if (j.getJobId().equals(job.getParentId())){
                        childrenList.add(job);
                        }
                    } );
            j.setChildrenJob(childrenList);
        }

        return jobParentList;
    }
}
